#!/usr/bin/env node

// GPIO写入调试脚本
const { exec } = require('child_process');
const { promisify } = require('util');
const execAsync = promisify(exec);

console.log("🔍 GPIO写入调试脚本");

async function testGPIOOperations() {
  try {
    console.log("\n🔧 测试GPIO操作...");
    
    // 1. 检查引脚当前状态
    console.log("1. 检查引脚13当前状态:");
    try {
      const readResult = await execAsync('gpio -g read 13');
      console.log(`   当前状态: ${readResult.stdout.trim()}`);
    } catch (error) {
      console.log(`   读取失败: ${error.message}`);
    }
    
    // 2. 设置引脚为输出模式
    console.log("2. 设置引脚13为输出模式:");
    try {
      await execAsync('gpio -g mode 13 out');
      console.log("   设置成功");
    } catch (error) {
      console.log(`   设置失败: ${error.message}`);
    }
    
    // 3. 再次检查引脚状态
    console.log("3. 再次检查引脚13状态:");
    try {
      const readResult = await execAsync('gpio -g read 13');
      console.log(`   当前状态: ${readResult.stdout.trim()}`);
    } catch (error) {
      console.log(`   读取失败: ${error.message}`);
    }
    
    // 4. 写入高电平
    console.log("4. 写入高电平到引脚13:");
    try {
      await execAsync('gpio -g write 13 1');
      console.log("   写入成功");
    } catch (error) {
      console.log(`   写入失败: ${error.message}`);
    }
    
    // 5. 立即检查引脚状态
    console.log("5. 立即检查引脚13状态:");
    try {
      const readResult = await execAsync('gpio -g read 13');
      console.log(`   当前状态: ${readResult.stdout.trim()}`);
    } catch (error) {
      console.log(`   读取失败: ${error.message}`);
    }
    
    // 6. 等待一段时间后再次检查
    console.log("6. 等待1秒后再次检查引脚13状态:");
    await new Promise(resolve => setTimeout(resolve, 1000));
    try {
      const readResult = await execAsync('gpio -g read 13');
      console.log(`   当前状态: ${readResult.stdout.trim()}`);
    } catch (error) {
      console.log(`   读取失败: ${error.message}`);
    }
    
    // 7. 写入低电平
    console.log("7. 写入低电平到引脚13:");
    try {
      await execAsync('gpio -g write 13 0');
      console.log("   写入成功");
    } catch (error) {
      console.log(`   写入失败: ${error.message}`);
    }
    
    // 8. 检查最终状态
    console.log("8. 检查引脚13最终状态:");
    try {
      const readResult = await execAsync('gpio -g read 13');
      console.log(`   当前状态: ${readResult.stdout.trim()}`);
    } catch (error) {
      console.log(`   读取失败: ${error.message}`);
    }
    
    // 9. 检查是否有其他进程占用GPIO
    console.log("9. 检查是否有其他进程占用GPIO:");
    try {
      const psResult = await execAsync('ps aux | grep gpio');
      console.log("   相关进程:");
      psResult.stdout.split('\n').forEach(line => {
        if (line && !line.includes('grep')) {
          console.log(`     ${line}`);
        }
      });
    } catch (error) {
      console.log(`   检查失败: ${error.message}`);
    }
    
    // 10. 检查WiringPi版本
    console.log("10. 检查WiringPi版本:");
    try {
      const versionResult = await execAsync('gpio -v');
      console.log(`    ${versionResult.stdout.split('\n')[0]}`);
    } catch (error) {
      console.log(`    检查失败: ${error.message}`);
    }
    
  } catch (error) {
    console.error("❌ 测试过程中发生错误:", error.message);
  }
}

async function main() {
  console.log("🚀 开始GPIO写入调试");
  
  await testGPIOOperations();
  
  console.log("\n📋 调试完成");
}

main();